
students_scores = []


def record_score():
    while True:
        print("=====记录学生成绩=====")
        name = input("请输入学生姓名：")
        id_num = input("请输入学生学号：")

        for student in students_scores:
            if student['学号'] == id_num:
                print("学号已存在，请重新输入！")
                break
        else:
            break
    course = input("请输入课程名称：")
    while True:
        try:
            score = float(input("请输入成绩(0-100)："))
            if 0 <= score <= 100:
                break
            else:
                print("成绩必须在0到100之间，请重新输入！")
        except ValueError:
            print("输入无效，请输入一个有效的数字！")
    students_scores.append({
        '姓名': name,
        '学号': id_num,
        '课程': course,
        '成绩': score
    })
    print("成绩已成功记录！")


def query_score():
    print("=====查询学生成绩=====")
    print("请选择查询方式：")
    print("1. 按学生姓名查询")
    print("2. 按学生学号查询")
    print("3. 按课程名称查询")
    choice = input("请输入选项序号：")
    if choice == "1":
        name = input("请输入学生姓名：")
        found = False
        for student in students_scores:
            if student['姓名'] == name:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
                found = True
        if not found:
            print(f"未找到姓名为 {name} 的学生成绩记录。")
    elif choice == "2":
        id_num = input("请输入学生学号：")
        found = False
        for student in students_scores:
            if student['学号'] == id_num:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
                found = True
        if not found:
            print(f"未找到学号为 {id_num} 的学生成绩记录。")
    elif choice == "3":
        course = input("请输入课程名称：")
        for student in students_scores:
            if student['课程'] == course:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
    else:
        print("无效的查询选项，请重新运行程序选择正确选项。")
def main_menu():
        while True:
            print("===========================")
            print("欢迎使用学生成绩管理系统")
            print("===========================")
            print("请选择操作：")
            print("1. 记录学生成绩")
            print("2. 查询学生成绩")
            print("3. 统计课程成绩")
            print("4. 退出系统")
            choice = input("请输入选项序号：")
            if choice == "1":
                record_score()
            elif choice == "2":
                query_score()
            elif choice == "3":
                statistics_score()
            elif choice == "4":
                print("感谢使用学生成绩管理系统，再见！")
                break
            else:
                print("无效的选项，请重新输入！")

if __name__ == "__main__":
    main_menu()
# 统计课程成绩函数
def statistics_score():
    print("=====统计课程成绩=====")
    course = input("请输入课程名称：")
    scores = [student['成绩'] for student in students_scores if student['课程'] == course]
    if scores:
        average = sum(scores) / len(scores)
        highest = max(scores)
        lowest = min(scores)
        print(f"课程：{course}")
        print(f"平均分：{average:.2f}")
        print(f"最高分：{highest}")
        print(f"最低分：{lowest}")
    else:
        print(f"未找到 {course} 课程的成绩记录。")


# 主菜单函数
def main_menu():
    while True:
        print("===========================")
        print("欢迎使用学生成绩管理系统")
        print("===========================")
        print("请选择操作：")
        print("1. 记录学生成绩")
        print("2. 查询学生成绩")
        print("3. 统计课程成绩")
        print("4. 退出系统")
        choice = input("请输入选项序号：")
        if choice == "1":
            record_score()
        elif choice == "2":
            query_score()
        elif choice == "3":
            statistics_score()
        elif choice == "4":
            print("感谢使用学生成绩管理系统，再见！")
            break
        else:
            print("无效的选项，请重新输入！")
if __name__ == "__main__":
    main_menu()
//修改
